*  create linear growth trend rates for PWT and WDI gdppw data

cd "~/Documents/econ/research/current/cognitive/estimation/data"

// ------------- 5-year panel 1960-2015 PWT --------------- //
// ---  + 1960-2010 + 1980-2010 + 1985-2010 + 1990-2010  ---//

local pwt "~/Documents/econ/data/macro/XCountry/PWT/pwt10/pwt100"
local GDPgvar "rgdpna/emp"
local GDPlab "PWT GDP p.w."

local delt 5
local year0 1960
local yearT 2020

use countrycode year rgdpna emp using "`pwt'", clear
gen double lgdppw = ln(`GDPgvar') 
local yearT_1 = `yearT'-`delt'
global fn`delt'		// reset value of filename macro
forvalues y = `year0'(`delt')`yearT_1' {
	local y1 = `y' + `delt'
	di "y = `y'; y1 = `y1'"
	capture quietly parmby "regr lgdppw year if year>=`y' & year<=`y1'", ///
		by(countrycode) idnum(`y') saving(g`delt'`y', replace) 	///
		flist(fn`delt')	
}

drop *
di "flist = ${fn`delt'}"
ls g5*
append using ${fn`delt'}
drop if dof < 3 // no important countries dropped this way
gen double gPWT`delt' = estimate*100
label var gPWT`delt' "`GDPlab' `delt' year growth, PWT `GDPgvar'"
keep countrycode idnum parm gPWT`delt'
	
keep if parm=="year"
drop parm
rename idnum year
label var year
sort countrycode year

// merge in initial year GDP
merge 1:1 countrycode year using "`pwt'", keep(match) keepusing(rgdpo emp pop)
gen double lgdppw = ln(rgdpo/emp)
label var lgdppw "log `GDPlab', PWT rgdpo/emp"
gen double lgdppc = ln(rgdpo/pop)
label var lgdppc "log GDP per capita, PWT rgdpo/pop"
drop _merge rgdpo emp pop
sort countrycode year

// merge in 25 year lagged GDP
replace year = year-25
merge 1:1 countrycode year using "`pwt'", keep(master match) keepusing(rgdpo emp)
gen double lgdppwL25 = ln(rgdpo/emp)
label var lgdppwL25 "log `GDPlab', PWT lagged 25 years"
drop _merge rgdpo emp
replace year = year+25
sort countrycode year

save gdp, replace


// ------------- 5-year panel 1960-2015 WDI --------------- //
// ---  + 1960-2010 + 1980-2010 + 1985-2010 + 1990-2010  ---//
// ---  + 1960-2015 + 1985-2015 + 1990-2015  ---//

local GDPlab "WDI PPP GDP p.w"
local delt 5
local year0 1960
local yearT 2020

use countrycode year gdp* using WDI, clear
merge 1:1 countrycode year using "`pwt'", keep(match) keepusing(emp) nogen
gen double lgdppwWDI = ln(gdpWDI/emp)
keep countrycode year lgdppwWDI
sort countrycode year 
save lgdpWDI, replace
local yearT_1 = `yearT'-`delt'
global fn`delt'		// reset value of filename macro
forvalues y = `year0'(`delt')`yearT_1' {
	local y1 = `y' + `delt'
	di "y = `y'; y1 = `y1'"
	capture quietly parmby "regr lgdppwWDI year if year>=`y' & year<=`y1'", ///
		by(countrycode) idnum(`y') saving(g`delt'`y', replace) 	///
		flist(fn`delt')	
}

drop *
append using ${fn`delt'}
drop if dof < 3 // no important countries dropped this way
gen double gWDI`delt' = estimate*100
label var gWDI`delt' "`GDPlab' `delt' year growth"
keep countrycode idnum parm gWDI`delt'

keep if parm=="year"
drop parm
rename idnum year
label var year
sort countrycode year

merge 1:1 countrycode year using lgdpWDI, keep(master match) nogen
label var lgdppwWDI "log `GDPlab'"
rename lgdppwWDI lgdppwWDI0
sort countrycode year

// merge in 25 year lagged GDP
replace year = year-25
merge 1:1 countrycode year using lgdpWDI, keep(master match) nogen
rename lgdppwWDI lgdppwWDIL25
label var lgdppwWDIL25 "log `GDPlab', lagged 25 years"
rename lgdppwWDI0 lgdppwWDI 
replace year = year+25
sort countrycode year

merge 1:1 countrycode year using gdp
drop _merge
order countrycode year gPWT5 lgdppw lgdppwL25 lgdppc
sort countrycode year
compress
saveold gdp, version(11) replace

forvalues y = `year0'(`delt')`yearT_1' {
	erase g`delt'`y'.dta
}
erase lgdpWDI.dta
